import axios from "axios";
// import router from "../router";
import { stringify } from "qs";
// import { Message } from "element-ui";
// import "element-ui/lib/theme-chalk/index.css";
import qs from "qs";
// import Cookies from 'js-cookie'


// 初始化axios service
const service = axios.create({
  // baseURL: " http://172.25.35.62:10010/api", //
  timeout: 8000,
});

axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

// Add a response interceptor
axios.interceptors.response.use(function (response) {
  // Do something with response data
  return response;
}, function (error) {
  // Do something with response error
  return Promise.reject(error);
});
// 封装请求方法
const request = async (
  url = "",
  data = {},
  type = "GET",
  headers = {},
  qss = false
) => {
  type = type.toUpperCase();
  let res;
  // const token = getToken() || '';
  if (type == "GET") {
    if (qss) {
      res = await service.get(`${url}?${stringify(data)}`);
    } else {
      res = await service.get(url, { params: data });
    }
  } else if (type == "POST") {
    res = await service.post(`${url}`, qss ? qs.stringify(data) : data, {
      headers: headers,
    });
  } else if (type == "DELETE") {
    res = await service.delete(url, data);
  } else if (type == "PUT") {
    // res = await service.put(`${url}?token=${token}`, data);
    res = await service.put(`${url}`, data);
  } else if (type == "PATCH") {
    res = await service.patch(url, data);
  }
  if (res.data.code === 200) {
    // 正确返回
    // console.log(this.Cookies,111);
    return res.data;
  }
  else {
    return  res.data;

  }
};
export default request;
